<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="6">
            <a-form-item label="考核名称">
              <a-input placeholder="请输入" v-model="queryParam.checkName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="计划年度">
              <a-date-picker
                mode="year"
                placeholder="请选择年份"
                format="YYYY"
                :allowClear="false"
                value-format="yyyy-MM-dd"
                v-model="queryParam.checkYear"
                :open="yearPickShow"
                style="width: 100%"
                @panelChange="handlePanelChange"
                @openChange="handleOpenChange"
              />
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      @change="handleTableChange"
    >
      <!--TODO 角色权限 添加-->
      <span slot="actionName">
        <a-button :disabled="role == 1" size="small" @click="openCreatModel()" type="primary" icon="plus"
          >新增</a-button
        >
      </span>

      <span slot="status" slot-scope="text, record">
        <span v-if="text != '3'">{{ status[text] }}</span>
        <a v-else @click="handleLookAudit(record, '审批详情')">{{ status[text] }}(查看原因)</a>
      </span>
      <span slot="action" slot-scope="text, record">
        <a @click="handleDetail(record, '')">查看</a>
        <template v-if="role == record.orgLevel && (record.status == 0 || record.status == 3)">
          <a-divider type="vertical" />
          <a @click="handleEdit(record, '')">编辑</a>
        </template>

        <template v-if="record.approveState != '4'&& role !='1'">
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="handleTableDel(record)">
            <a>删除</a>
          </a-popconfirm>
        </template>

        <!-- <a-divider type="vertical" /> -->
        <!--TODO 角色权限 编辑 -->
        <!-- <a-dropdown>
          <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
          <a-menu slot="overlay">
          
            <a-menu-item v-if="role != 1 && record.status == 0">
              <a @click="handleEdit(record, '')">编辑</a>
            </a-menu-item>
            <a-menu-item v-if="record.approveState != '4'">
              <a-popconfirm title="确定删除吗?" @confirm="handleTableDel(record)">
                <a>删除</a>
              </a-popconfirm>
            </a-menu-item>
          </a-menu>
        </a-dropdown> -->
      </span>
    </a-table>
    <!-- table区域-end -->

    <create ref="creatModel" @openModelView="handleAddSubmit"></create>
    <edit-model :role="role" ref="modalForm" @updated="handleModelSubmit"></edit-model>
    <CheckAndEditModel ref="auditView"></CheckAndEditModel>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import columns from './indexColumns'
import create from './create'
import { httpAction, getAction, postAction, deleteAction } from '@/api/manage'
import moment from 'moment'
import editModel from './editModel.vue'
import { putAction } from '@/api/manage'
import CheckAndEditModel from './CheckAndEditModel.vue'

export default {
  name: 'jh',
  mixins: [JeecgListMixin],
  components: {
    create,
    editModel,
    CheckAndEditModel,
  },
  data() {
    return {
      // 表头
      columns: columns(this),
      url: {
        list: 'performcheck/checkDeptInfo/queryDeptList',
        add: 'performcheck/checkDeptInfo/insertDept',
        del: 'performcheck/checkDeptInfo/deleteDept',
        role: 'performcheck/checkDeptInfo/getOrgLevel',
      },
      status: {
        0: '待提交',
        1: '待审核',
        2: '已通过',
        3: '已驳回',
      },
      yearPickShow: false,
      role: '3',
      dataSource: [],
    }
  },
  mounted() {
    this.getRole()
  },
  computed: {
    modalFormShow() {
      return this.$refs.modalForm.visible
    },
  },
  methods: {
    handleLookAudit(record, name) {
      this.$refs.auditView.check(record)
      this.$refs.auditView.title = name
      // this.$refs.modalForm.disableSubmit = true
    },
    openCreatModel() {
      this.$refs.creatModel.visible = true
    },
    async getRole() {
      this.role = await getAction(this.url.role)
    },
    // 重新加载
    handleModelSubmit() {
      this.loadData()
    },
    handleAddSubmit(evt) {
      return new Promise((re, rj) => {
        postAction(this.url.add, evt).then((res) => {
          console.log(res, 'res')
          if (res.success) {
            this.loadData()
            this.$message.success(res.message)
            re(res.success)
          } else {
            rj(res.success)
            this.$message.error(res.message)
          }
        })
      })
    },
    handlePanelChange(value) {
      this.queryParam.checkYear = moment(value).format('YYYY')
      this.yearPickShow = false
      this.$refs.form.validateField('checkYear', (error) => {
        console.log(error)
      })
    },
    // 弹出日历和关闭日历的回调
    handleOpenChange(status) {
      this.yearPickShow = status
      if(this.queryParam.checkYear){
        this.queryParam.checkYear = moment(this.queryParam.checkYear.slice(0, 4)).format('YYYY')
      }
    },
    async handleTableDel(row) {
      const data = await putAction(`${this.url.del}?id=${row.id}`)
      if (data.success) {
        this.$message.success(data.message)
        this.loadData()
      } else {
        this.$message.error(data.message)
      }
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>